package com.wuyoucar.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.netflix.discovery.converters.Auto;
import com.wuyoucar.common.PageBean;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TBank;
import com.wuyoucar.entity.TReport;
import com.wuyoucar.entity.TUser;
import com.wuyoucar.mapper.PolicyMapper;
import com.wuyoucar.mapper.ReportMapper;
import com.wuyoucar.mapper.TBankMapper;
import com.wuyoucar.mapper.TUserMapper;
import com.wuyoucar.service.IReportService;
import com.wuyoucar.service.InMailservice;
import com.wuyoucar.service.InTBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private TUserMapper tUserMapper;

    @Autowired
    private TBankMapper tBankMapper;
    @Autowired
    private InTBankService inTBankService;
    @Autowired
    private InMailservice inMailservice;
    @Autowired
    private PolicyMapper policyMapper;

    //获取为处理的保单号
    @Override
    public ResultBean getReport(int pageIndex,int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> report = reportMapper.getReport();
        Page<TReport> tReportPage = (Page<TReport>) report;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());

        if (report != null) {
            return new ResultBean(200, "报案单查询成功", pb);
        }
        return new ResultBean(404, "暂时没有未处理的报案单");
    }

    @Override
    public ResultBean getDisPatcher() {
        List<TUser> list = tUserMapper.getDispacher();
        if (list != null) {
            return new ResultBean(200, "查询成功", list);
        }
        return new ResultBean(404, "暂时没有勘察人员");
    }

    //根据报案号派遣勘察人员进行勘察
    @Override
    public ResultBean toDispatcher(Integer reportid, Integer userid) {
        if (userid == 0) {
            return new ResultBean(404, "请选择派遣人员姓名");
        }
        int n = reportMapper.updateReport(reportid, userid);
        return new ResultBean(200, "派遣成功");
    }

    @Override
    public ResultBean showDispatcher(int pageIndex,int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> report = reportMapper.showDispatcher();
        Page<TReport> tReportPage = (Page<TReport>) report;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        if (report != null) {
            return new ResultBean(200, "报案单查询成功", pb);
        }
        return new ResultBean(404, "暂时没有已派遣的报案单");
    }


    //查看已通过查勘的所有申请
    @Override
    public ResultBean getLossAssessment() {
        int pageIndex = 1;
        int pageSize = 8;
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> reports = reportMapper.getLossAssessment();
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        if (reports.size() > 0) {
            return new ResultBean(0, "查询成功", pb);
        }
        return new ResultBean(404, "暂时没有已通过查勘的申请", pb);
    }

    //查看已通过查勘的所有申请1
    @Override
    public ResultBean getLossAssessment(int pageIndex,int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> reports = reportMapper.getLossAssessment();
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        if (reports.size() > 0) {
            return new ResultBean(0, "查询成功", pb);
        }
        return new ResultBean(404, "暂时没有已通过查勘的申请", pb);
    }

    //查看所有状态为待审核的案件
    @Override
    public ResultBean getAllReport() {
        List<TReport> allReport = reportMapper.getAllReport();
        if (allReport.size() > 0) {
            return new ResultBean(0, "查询成功", allReport);
        }
        return new ResultBean(404, "暂时没有案件");
    }


    //查看所有状态为待复审的案件
    @Override
    public  PageBean<TReport> getAllReportByStates(Integer pageIndex, Integer pageSize, TReport tReport) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> reportbyReportid = reportMapper.getReportbyReportid(tReport);
        Page<TReport> list = (Page<TReport>) reportbyReportid;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;

    }

    //定损条件查询
    @Override
    public ResultBean condition(int pageIndex, int pageSize,TReport tReport) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> list = reportMapper.condition(tReport);
        Page<TReport> tReportPage = (Page<TReport>) list;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        if (list.size() > 0) {
            return new ResultBean(0, "查询成功", pb);
        }
        return new ResultBean(404, "暂时没有您所需要的数据", pb);
    }

    //定损
    @Override
    public ResultBean updateLossAssessment(TReport tReport) {
        ResultBean resultBean = new ResultBean();
        Date date = new Date();
        tReport.setLastmodifytime(date);
        int n = reportMapper.updateLossAssessment(tReport);
        if (n > 0) {
            if (tReport.getReportstate() == "定损驳回" || "定损驳回".equals(tReport.getReportstate())) {
                resultBean.setCode(101);
                resultBean.setMsg("已驳回");
            } else {
                resultBean.setCode(0);
                resultBean.setMsg("已通过");
            }
        } else {
            resultBean.setCode(-1);
            resultBean.setMsg("定损失败！");
        }
        return resultBean;
    }

    //查看报案详情
    @Override
    public ResultBean getReportDetails(Integer reportid) {
        ResultBean resultBean = new ResultBean();
        TReport tReport = reportMapper.getReportDetails(reportid);
        if (tReport != null) {
            resultBean.setCode(0);
            resultBean.setData(tReport);
        } else {
            resultBean.setCode(-1);
            resultBean.setMsg("查询失败");
        }
        return resultBean;
    }

    //查询状态为"待放款"的理赔单
    @Override
    public ResultBean selectReportBystate() {
        List<TReport> reports = reportMapper.selectReportByState("待放款","闪赔");
        if (reports.size() != 0) {
            return new ResultBean(200, "查询成功", reports);
        } else {
            return new ResultBean(404, "查询失败");
        }

    }

    //查询状态为"已放款"的理赔单
    @Override
    public ResultBean selectReportState() {
        List<TReport> reports = reportMapper.selectReportBySatae2("已放款");
        if (reports.size() != 0) {
            return new ResultBean(200, "查询成功", reports);
        } else {
            return new ResultBean(404, "查询失败");
        }

    }

    //待放款初始化分页
    @Override
    public PageBean<TReport> selectReportByPage() {
        int pageIndex = 1;
        int pageSize = 8;
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> reports = reportMapper.selectReportByState("待放款","闪赔");
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal(),
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        return pb;
    }

    //待放款初始化分页
    @Override
    public PageBean<TReport> selectReportByPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> reports = reportMapper.selectReportByState("待放款","闪赔");
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal(),
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        return pb;
    }
    //待放款查询分页
    @Override
    public PageBean<TReport> searchByMoney(Integer pageIndex, Integer pageSize, Double money1, Double money2) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> tReports = reportMapper.searchByMoney(money1,money2,"待放款","闪赔");
        Page<TReport> list = (Page<TReport>) tReports;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;
    }
    //已放款查询分页
    @Override
    public PageBean<TReport> searchByMoney2(Integer pageIndex, Integer pageSize, Double money1, Double money2) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> tReports = reportMapper.searchByMoney2(money1,money2,"已放款");
        Page<TReport> list = (Page<TReport>) tReports;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;
    }



    //放款后修改状态
    @Override
    public ResultBean updateState(Double money, Integer reportid, Integer policynum) {
        Integer userid = policyMapper.getReportbyNum(policynum).getUserid();
        List<TBank> tBanks = tBankMapper.selectAllCard(userid);
        if (tBanks.size()==0){
            return new ResultBean(100,"用户尚未绑定银行卡无法放款");
        }else{
            String bankcard = tBanks.get(0).getBankcard();
            Double money1 = tBanks.get(0).getBankmoney()+money;
            Integer paytime = 1;
            ResultBean resultBean = inTBankService.updateMoney(bankcard,money1);
            ResultBean resultBean1 = inMailservice.addPaytime(userid, paytime);
            if (resultBean.getCode() == 200 ){
                String reportstate = "已放款";
                String remark = "已发放理赔金";
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date lastmodifytime = new Date();
                try {
                    //获取系统当前时间
                    lastmodifytime = df.parse(df.format(new Date()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int i1 = reportMapper.updateState(new TReport(reportid, reportstate,lastmodifytime,remark));
                if (i1 > 0 ){
                    return new ResultBean(200,"操作成功");
                }else{
                    return new ResultBean(404,"操作失败");
                }
            }else{
                return new ResultBean(-1,"操作失败");
            }

        }

    }
    //已放款初始化分页
    @Override
    public PageBean<TReport> selectPage() {
        int pageIndex = 1;
        int pageSize = 8;
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> reports = reportMapper.selectReportBySatae2("已放款");
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        return pb;
    }
    //已放款初始化分页
    @Override
    public PageBean<TReport> selectPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TReport> reports = reportMapper.selectReportBySatae2("已放款");
        Page<TReport> tReportPage = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());
        return pb;
    }


    @Override
    public ResultBean alterReport(String newName, Integer reportid, String remark) {
        if (reportMapper.alterReport(newName, reportid, remark) > 0) {
            return new ResultBean(200,"勘查提交");
        }
        return new ResultBean(200,"提交失败");
    }

    //审核分页
    @Override
    public PageBean<TReport> seachPage(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> allReportByreportno = reportMapper.getAllReportByreportno();
        Page<TReport> list = (Page<TReport>) allReportByreportno;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;
    }
    //审核初始化分页
    @Override
    public PageBean<TReport> seachPage() {
        Integer pageIndex = 1;
        Integer pageSize = 5;
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> reports = reportMapper.getAllReport();
        Page<TReport> list = (Page<TReport>) reports;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;
    }

    //报案号查询
    @Override
    public PageBean<TReport> getReportbyReportid(Integer pageIndex,Integer pageSize,TReport tReport) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> reportbyReportid = reportMapper.getReportbyReportid(tReport);
        Page<TReport> list = (Page<TReport>) reportbyReportid;
        PageBean<TReport> pb = new PageBean<TReport>(
                list.getTotal(),
                list.getResult(),
                list.getPageSize(),
                list.getPages(),
                list.getPageNum());
        return pb;

    }


    //修改订单状态（已驳回）
    @Override
    public ResultBean turndown(Integer policynum, Integer firstid,String remark) {
        int i = reportMapper.turndown(policynum, firstid,remark);
        if (1 > 0) {
            return new ResultBean(0, "修改成功", i);
        }
        return new ResultBean(1, "修改失败", i);
    }

    //查看已派遣的报案号
    @Override
    public ResultBean getHasSent(Integer surveyorid) {
        Integer pageIndex = 1;
        Integer pageSize = 8;
        PageHelper.startPage(pageIndex, pageSize);
        List<TReport> list = reportMapper.getHasSent(surveyorid);
        Page<TReport> tReportPage = (Page<TReport>) list;
        PageBean<TReport> pb = new PageBean<TReport>(tReportPage.getTotal() ,
                tReportPage.getResult(),
                tReportPage.getPageSize(),
                tReportPage.getPages(),
                tReportPage.getPageNum());

        if (list.size() > 0) {
            return new ResultBean(200, "查询成功", pb);
        }
        return new ResultBean(500, "查询失败", 0);
    }

    //修改审核状态
    @Override
    public ResultBean updateReportAudit(Integer policynum, Integer firstid) {
        int i = reportMapper.updateReportAudit(policynum, firstid);
        if (1 > 0) {
            return new ResultBean(0, "修改成功", i);
        }
        return new ResultBean(1, "修改失败", i);
    }

    //复审
    @Override
    public ResultBean updateRecheck(Integer policynum, Integer firstid) {
        int i = reportMapper.updateRecheck(policynum, firstid);
        if (1 > 0) {
            return new ResultBean(0, "修改成功", i);
        }
        return new ResultBean(1, "修改失败", i);
    }

    @Override
    public ResultBean passVerification(Integer policynum, Integer firstid) {
        int i = reportMapper.passVerification(policynum, firstid);
        if (1 > 0) {
            return new ResultBean(0, "修改成功", i);
        }
        return new ResultBean(1, "修改失败", i);
    }

    //查询已派遣的所有报案号带分页
    @Override
    public PageBean<TReport> page(Integer pageIndex,Integer pageSize,Integer surveyorid){
        //分页
        PageHelper.startPage(pageIndex, pageSize);//分页插件
        List<TReport> list = reportMapper.getHasSent(surveyorid);
        Page<TReport> pageList = (Page<TReport>) list;//分页插件
        PageBean<TReport> pb = new PageBean<TReport>(pageList.getTotal() ,
                pageList.getResult(),
                pageList.getPageSize(),
                pageList.getPages(),
                pageList.getPageNum());
        return pb;
    }
    //闪赔
    @Override
    public ResultBean sanPei(String damageImg, String signImg, Integer money, Integer reportid) {
        if(reportMapper.sanPei(damageImg, signImg, money, reportid)>0){
            return new ResultBean(1,"闪赔申请成功");
        }
        return new ResultBean(0,"闪赔申请失败");
    }

    //报案人查询
    @Override
    public PageBean<TReport> searchName(Integer pageIndex, Integer pageSize, String rname) {
        //分页
        PageHelper.startPage(pageIndex, pageSize);//分页插件
        List<TReport> list = reportMapper.searchName(rname);
        Page<TReport> pageList = (Page<TReport>) list;//分页插件
        PageBean<TReport> pb = new PageBean<>(pageList.getTotal() ,
                pageList.getResult(),
                pageList.getPageSize(),
                pageList.getPages(),
                pageList.getPageNum());
        return pb;
    }

}
